<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\BaseController;
use think\facade\Db;
use app\wx_pay\controller\JsApiPayV3;
class Index extends BaseController
{
    protected $table = 'submit';
    public function index()
    {
        return '您好！这是一个[api]示例应用';
    }
    public function refund(){
//        ^ array:12 [
//            "amount" => array:10 [
//            "currency" => "CNY"
//    "discount_refund" => 0
//    "from" => []
//    "payer_refund" => 1
//    "payer_total" => 1
//    "refund" => 1
//    "refund_fee" => 0
//    "settlement_refund" => 1
//    "settlement_total" => 1
//    "total" => 1
//  ]
//  "channel" => "ORIGINAL"
//  "create_time" => "2023-09-18T16:26:34+08:00"
//  "funds_account" => "AVAILABLE"
//  "out_refund_no" => "1324132e2f772a08d439fd239ddcf66a"
//  "out_trade_no" => "230918456753385472"
//  "promotion_detail" => []
//  "refund_id" => "50301207342023091899982559021"
//  "status" => "SUCCESS"
//  "success_time" => "2023-09-18T16:26:52+08:00"
//  "transaction_id" => "4200001937202309188905434548"
//  "user_received_account" => "支付用户零钱"
//]
        $data = request()->param();
        $JsApiPayV3 = new JsApiPayV3();
        $res = $JsApiPayV3->foundMoney($data['out_trade_no'],$data['pay_price'],(string)time(),'');
        if($res['status'] !=  "ABNORMAL"){
            Db::table('sign_order')->where('out_trade_no',$data['out_trade_no'])->update(['ispay'=>2]);
            success('退款','');
        }else{
            fail('退款','');
        }
    }
    public function getSubmit(){
        /**
        CREATE TABLE `submit` (
        `submit_id` int(11) NOT NULL,
        `submit_ago` varchar(100) NOT NULL,
        `submit_name` varchar(15) NOT NULL,
        `submit_phone` bigint(11) NOT NULL,
        `submit_time` datetime NOT NULL,
        `check_time` date NOT NULL,
        PRIMARY KEY (`submit_id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;

        --
        -- 转存表中的数据 `submit`
        --

        INSERT INTO `submit` (`submit_id`, `submit_ago`, `submit_name`, `submit_phone`, `submit_time`, `check_time`) VALUES
        (6, '30岁以下', '111', 15555555555, '2022-10-17 17:09:35', '2022-10-17'),
        (2, '50以上', '牛皮', 18845455445, '2022-10-11 16:52:25', '2022-10-10'),
        (3, '50以上', '老王', 18877848545, '2022-10-12 09:53:18', '2022-10-12'),
        (4, '30岁以下', '米奇', 16797994664, '2022-10-12 15:35:49', '2022-10-12'),
        (5, '50以上', '川普', 19949976464, '2022-10-12 16:44:42', '2022-10-12'),
        (7, '30岁以下', '陈会', 18826964910, '2022-10-22 14:35:32', '2022-10-22'),
        (8, '30-40岁', '牛中文', 13406293548, '2022-10-24 15:13:47', '2022-10-24');

         **/
        $data = request()->param();
        $page = (int)$data['page'];
        $limit = (int)$data['limit'];
        $result2 = [];
        $result2['submit_name'] = ['=',$data['submit_name']??''];
        $result2['submit_phone'] = ['=',$data['submit_phone']??''];
        $result2['ispay'] = ['=',$data['ispay']??''];
        $result2['special_status'] = ['=',$data['special_status']??''];
        $result2['out_trade_no'] = ['=',$data['out_trade_no']??''];
        $result2['submit_time'] =  ['between', [$data['date1'] ?? '',$data['date2'] ?? '']];
        $result2['view_id'] = ['=',$data['view_id']??''];
        $result2['del_status'] = ['=',0];
        $w = array();
        foreach($result2 as $k=>$v) {
            if(is_array($v[1])){
                $start_time = $v[1][0];
                $end_time = $v[1][1];
                if($start_time!=""&&$end_time!=""){
                    $w[] = "$k $v[0] '$start_time' AND '$end_time'";
                }
            }else{
                if($v[1]!==""){
                    $w[] = "$k $v[0] '$v[1]'";
                }
            }

        }
        $where = join(' and ', $w);
        if($data['view_id'] == 1){
            $table_data = Db::table($this->table)
                ->alias('s')
                ->leftJoin('sign_order o','o.uid=s.submit_id')
                ->filter(function ($_data){
                    $special = [0=>'否',1=>'特殊学生'];
                    $_data['special_status'] = $special[$_data['special_status']];
                    $picture = Db::table('picture')->where('picture_id',$_data['picture_id'])->find();
                    if(empty($picture)){
                        $_data['subject_name'] = '当前科目已被删除！';
                    }else{
                        $_data['subject_name'] = $picture['picture_url'];
                    }
                    //是否支付数据
//                    $pay_data = Db::table('sign_order')->where('uid',$_data['submit_id'])->find();
//                    $_data['out_trade_no'] = $pay_data['out_trade_no'];
                    $pay_status = [0=>'未支付',1=>'已支付',2=>'已退款'];
                    $_data['ispay'] = $pay_status[$_data['ispay']];
//                    $_data['pay_time'] = $pay_data['pay_time'];
//                    $_data['pay_price'] = $pay_data['pay_price'];
                    return $_data;
                })
                ->page($page,$limit)
                ->where($where)
                ->select();
            $table_total = Db::table($this->table)
                ->alias('s')
                ->leftJoin('sign_order o','o.uid=s.submit_id')
                ->where($where)
                ->count();
        }else{
            $table_data = Db::table($this->table)
                ->alias('s')
                ->leftJoin('sign_order o','o.uid=s.submit_id')
                ->filter(function ($_data){
                    $pay_status = [0=>'未支付',1=>'已支付',2=>'已退款'];
                    $_data['ispay'] = $pay_status[$_data['ispay']];
                    return $_data;
                })
                ->page($page,$limit)
                ->where($where)
                ->select();
            $table_total = Db::table($this->table)
                ->alias('s')
                ->leftJoin('sign_order o','o.uid=s.submit_id')
                ->where($where)
                ->count();
        }
        echo json_encode([
            'status' => 200,
            'msg' => 'ok',
            'data' => $table_data,
            'total' => $table_total
        ]);
        exit;

    }
    public function delList(){
        $data = request()->param();
        $this->delData($this->table,['submit_id'=>$data['submit_id']]);
    }
	public function delAllList(){

        $this->delAllData($this->table,[['submit_id','>',0]]);

//        if($checkToken['status']==10001){
//           $data = request()->param();
//           $token = request()->header('token');
//           $checkToken = checkToken($token);
//           $uid = $checkToken['data']->uid;
//           $admin_password = Db::table('admin')->where('admin_id',$uid)->value('admin_password');
//           $check_data = checkPassword($data['admin_password'],$admin_password);
//            if($check_data['status']==500){
//                return json(['status'=>500,'msg'=>'密码错误！']);
//            }
//            $all_del =  Db::table('submit')->where('submit_id','>',0)->update(['del_status'=>1]);
//            if($all_del){
//                return json(['status'=>200,'msg'=>'全部删除成功！']);
//            }else{
//                return json(['status'=>500,'msg'=>'全部删除失败！']);
//            }
//
//        }else{
//            return json(['status'=>501,'msg'=>$checkToken['data']]);
//        }
    }
}
